/*
 * @FileName: [PermissionTag.java]
 * 
 * @Package com.bcinfo.upp.admin.tag
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of BCINFO
 * Technology Limited Company ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in accordance
 * with the terms of the contract agreement you entered into with RKY.
 * 
 * $Rev: 44 $ $LastChangedDate: 2013-10-12 10:19:57 +0800 (星期六, 12 十月 2013) $
 * $LastChangedBy: wanggc $
 * 
 * @category bcinfo-upp
 * 
 * @version 1.1
 * 
 * @author $Author: wanggc $
 * 
 * Change History:[Formatter: author date description] <br/> 1 2 3
 */

package com.bcinfo.iccard.admin.tag;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.bcinfo.iccard.admin.common.Constants;
import com.bcinfo.upp.ac.model.Operator;
import com.bcinfo.upp.ac.model.RolePermission;
import com.bcinfo.upp.ac.util.PermissionUtil;

/**
 * 用与控制某个标签是否显示 <br/>
 * <p>
 * Description:有改标签功能的权限才输出html，没有则不输出<br/>
 * <p>
 * 
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      <ex:permissionShow actionBitVlaue="1" resourceTypeId="2">
 *      <a onclick="clickaddUser()" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
 *      </ex:permissionShow>
 * </PRE>
 * <p>
 */
public class PermissionTag extends BodyTagSupport
{

    private static final long serialVersionUID = -3150056647383422501L;

    private String            _actionCode;

    /*
     * (non-Javadoc) Description: <br/>
     * 
     * @return
     * 
     * @throws JspException
     * 
     * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
     */
    @SuppressWarnings("unchecked")
    @Override
    public int doStartTag() throws JspException
    {

        HttpServletRequest req = (HttpServletRequest) this.pageContext.getRequest();

        Integer _actionBit = Constants.RESOURCE_ACTION_MAP.get(_actionCode);
        if (_actionBit == null)
        {

            // 不输出内部标签
            return SKIP_BODY;
        }

        Long _resId = Constants.RESOURCE_RESOURCE_MAP.get(_actionCode);

        HttpSession se = req.getSession();
        Map<Long, RolePermission> map = (Map<Long, RolePermission>) se.getAttribute(Constants.CURRENT_ROLE_PERMISSION);
        Operator oper = (Operator) se.getAttribute(Constants.CURRENT_USER);

        if (oper == null)
        {
            // 不输出内部标签
            return SKIP_BODY;
        }
        if (oper.getOwner() == -1)
        {// 超级管理员
         // 输出内部标签
            return EVAL_BODY_INCLUDE;
        }
        if (map == null)
        {
            // 不输出内部标签
            return SKIP_BODY;
        }
        RolePermission per = map.get(_resId);
        if (per == null)
        {
            // 不输出内部标签
            return SKIP_BODY;
        }
        if (PermissionUtil.hasPermission(per.getActions(), _actionBit))
        {

            // 输出内部标签
            return EVAL_BODY_INCLUDE;
        }
        else
        {
            // 不输出内部标签
            return SKIP_BODY;

        }

    }

    public String getActionCode()
    {
        return _actionCode;
    }

    public void setActionCode(String actionCode)
    {
        _actionCode = actionCode;
    }

}
